package com.lemon.tuvi2015;

import java.util.ArrayList;

import org.json.JSONException;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.ProgressBar;

import com.lemon.tuvi2015.adapter.ZodiacAdapter;
import com.lemon.tuvi2015.object.GlobalObject;
import com.lemon.tuvi2015.util.BaseEngine;
import com.lemon.tuvi2015.util.CMD;
import com.lemon.tuvi2015.util.JsonParser;
import com.lemon.tuvi2015.util.TagName;
import com.lemon.tuvi2015.view.CustomActionbar;

public class ZodiacActivity extends Activity {
	CustomActionbar actionbar;
	GridView gridView;
	ArrayList<GlobalObject> listZodiac;
	String ID;
	View main; 
	ProgressBar progressBar;
//	private AdView adView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.congiap_layout);
		initView();
		initActionbar();

		Bundle extras = getIntent().getExtras();
		if (getIntent().hasExtra(TagName.KEY))
			ID = (String) extras.getString(TagName.KEY);
		if (ID != null && !ID.equals(""))
			new AsyncTaskParseJson().execute("");
		gridView.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				BaseEngine.startDetailsActivityObject(ZodiacActivity.this, listZodiac.get(position),"Tuổi ");
			}
		});
	}

	void initView() {
		progressBar=(ProgressBar) findViewById(R.id.progressBar);
		gridView = (GridView) findViewById(R.id.gridView_congiap);
		main=findViewById(R.id.main);
	}

	void initActionbar() {
		actionbar = (CustomActionbar) findViewById(R.id.header);
		OnClickListener listenerBack = new OnClickListener() {

			@Override
			public void onClick(View v) {
				onBackPressed();
			}
		};
		OnClickListener listenerShare = new OnClickListener() {

			@Override
			public void onClick(View v) {
				BaseEngine.shareBitmap(ZodiacActivity.this,  BaseEngine.ConvertViewtoBitmap(ZodiacActivity.this, main));
			}
		};
		actionbar.setActionbar(R.string.home_menu_congiap, listenerBack, listenerShare);
	}
	public static void showLogformLongString(String _tag, String str) {
		String tag = "GetJson";
		if (_tag != null && !_tag.equals("")) {
			tag = _tag;
		}
		if (str != null)
			if (str.length() > 4000) {
				Log.e(tag, "str.length = " + str.length());
				int chunkCount = str.length() / 4000; // integer division
				for (int i = 0; i <= chunkCount; i++) {
					int max = 4000 * (i + 1);
					if (max >= str.length()) {
						Log.e(tag, "chunk " + i + " of " + chunkCount + ":" + str.substring(4000 * i));
					} else {
						Log.e(tag, "chunk " + i + " of " + chunkCount + ":" + str.substring(4000 * i, max));
					}
				}
			} else {
				Log.e(tag, str.toString());
			}
	}

	public class AsyncTaskParseJson extends AsyncTask<String, String, String> {
		@Override
		protected void onPreExecute() {
		}

		@Override
		protected String doInBackground(String... arg0) {
			String str = JsonParser.getJson(CMD.HOME_ITEM_DETAILS, ID);
			
			return str;
		}

		@Override
		protected void onPostExecute(String result) {
			if (result != null) {
				showLogformLongString("", result);
				progressBar.setVisibility(View.GONE);
				// BaseEngine.showLogformLongString("", jsonObject.toString());
				try {
					if (!JsonParser.readStatus(result).equals("")) {
						listZodiac = JsonParser.readlistZodiac(result);
						if (listZodiac != null && listZodiac.size() > 0) {
							gridView.setAdapter(new ZodiacAdapter(getApplicationContext(), listZodiac));
						}
					}
				} catch (JSONException e) {
					e.printStackTrace();
				}
			}
		}
	}

//	@Override
//	public void onResume() {
//		super.onResume();
//		if (adView != null) {
//			adView.resume();
//		}
//	}
//
//	@Override
//	public void onPause() {
//		if (adView != null) {
//			adView.pause();
//		}
//		super.onPause();
//	}
//
//	/** Called before the activity is destroyed. */
//	@Override
//	public void onDestroy() {
//		if (adView != null) {
//			// Destroy the AdView.
//			adView.destroy();
//		}
//		super.onDestroy();
//	}
}
